package web.controller.employer;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.CompanyService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.CompanyRequest;
import web.security.Protection;

/**
 * Created by liuchang on 2015/6/23.
 */
@RestController
@RequestMapping(Routes.Employer.COMPANY)
public class Company extends Base {

    private static final Logger logger = LoggerFactory.getLogger(Company.class);

    @Autowired
    private CompanyService companyService;

    /**
     * 获取商家自身的公司信息。
     * <p>
     * 为求职者获取公司信息的接口不在这里。
     *
     * @return
     */
    @Protection
    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse get() {
        long userid = getUserIdFromAuthHeader();
        return companyService.getByOwner(userid);
    }

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse save(@RequestBody CompanyRequest request) {
        ValidationResult result = request.validate();
        if (!result.isSuccess()) {
            return fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();
        domain.Company company = copy(request, domain.Company.class);
        company.setOwner(employer);

        return companyService.update(company);
    }

}
